<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  顶部  -->
		<top-tab :options="stockList" :isLocation="false" @stockChange="stockChange"></top-tab>
		<!--  列表  -->
		<template v-if="dataList && dataList.length > 0">
			<view class="panel" v-for="item in dataList" @click="goto(item.billNo)">
				<view class="panel_title">
					<view class="panel_title_left">{{ item.billNo }}</view>
				</view>
				<view class="panel_content">
					<view class="panel_content_row">
						<view class="panel_content_row_item">
							<view class="panel_content_row_item_title">创建人员：</view>
							<view class="panel_content_row_item_value">{{ item.createUserName }}</view>
						</view>
						<view class="panel_content_row_item">
							<view class="panel_content_row_item_title">创建时间：</view>
							<view class="panel_content_row_item_value">{{ timeFormat(item.createTime) }}</view>
						</view>
					</view>
					<view class="panel_content_row">
						<view class="panel_content_row_item">
							<view class="panel_content_row_item_title">原仓库：</view>
							<view class="panel_content_row_item_value">{{ item.stockNo }}</view>
						</view>
						<view class="panel_content_row_item">
							<view class="panel_content_row_item_title">原仓位：</view>
							<view class="panel_content_row_item_value">
								{{ item.locationNo }}</view>
						</view>
					</view>
					<view class="panel_content_row">
						<view class="panel_content_row_item">
							<view class="panel_content_row_item_title">至仓库：</view>
							<view class="panel_content_row_item_value">{{ item.toStockNo }}</view>
						</view>
						<view class="panel_content_row_item">
							<view class="panel_content_row_item_title">至仓位：</view>
							<view class="panel_content_row_item_value">
								{{ item.toLocationNo }}</view>
						</view>
					</view>
				</view>
			</view>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleAdd" class="footerBtn scan" type="primary"><i
						class="iconfont icon-jiahao btn_icon"></i>新增调拨
				</button>
			</view>
		</view>
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	import dayjs from 'dayjs'

	export default {
		components: {
			topTab,
			uniLoading,
			uniEmptyInfo,
		},
		data() {
			return {
				loading: false,
				dataList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				scaningShow: false,
				scanNo: null,
				isFouce: false,
				stockList: [],
				stockNo: '',
				locationNo: '',
				locationName: ''
			}
		},
		onShow() {
			if (this.stockNo) {
				this.initData()
			}
		},
		async onLoad() {
			await this.getStockList();
			this.initData()
		},
		methods: {
			/** 查询仓库*/
			async getStockList() {
				this.loading = true;
				try {
					let { res, stockList = [] } = await this.$api.getCustomerStock();
					if (!res) return;
					this.stockList = stockList;
					if (stockList.length) this.stockNo = stockList[0].stockNo;
				} finally {
					this.loading = false;
				}
			},
			/** 初始化数据 */
			initData() {
				if (!this.stockNo) return;
				this.pageConfig = {
					pageIndex: 0,
					pageSize: 20,
					bottomEnd: false,
					bottomLoading: true
				}
				this.getDataList()
			},
			
			timeFormat(e) {
				return dayjs(e).format('YYYY-MM-DD')
			},

			/** 获取数据 */
			async getDataList() {
				let { res, data = [] } = await this.$api.allocationGetUserTask({
					stockNo: this.stockNo
				});
				if (!res) return;
				this.dataList = data ?? [];
				this.loading = false;
				this.pageConfig.bottomEnd = true;
				this.pageConfig.bottomLoading = false;
				uni.stopPullDownRefresh();
			},
			
			/** 新增调拨 */
			handleAdd() {
				this.$navigateTo('/pages/storehouse/transferSource')
			},

			/** 编辑 */
			goto(e) {
				this.$navigateTo(`/pages/storehouse/transferEdit?billNo=${e}`)
			},

			/**
			 * 选择仓库
			 * @param e
			 */
			stockChange(e) {
				this.stockNo = this.stockList[e].stockNo
				this.initData()
			},

			/**
			 * 下拉刷新数据
			 */
			onPullDownRefresh() {
				this.initData()
			},

			/** 上滑加载 */
			onReachBottom() {
				if (!this.pageConfig.bottomEnd) {
					this.pageConfig.pageIndex++
					this.pageConfig.bottomLoading = true
					this.getDataList();
				}
			},
			
			/**
			 * 返回指定页面(onBackPress函数放置与data或onLoad同等位置)
			 *
			 * @param e
			 */
			onBackPress(e) {
			  uni.redirectTo({
			    url: "/pages/menu/menu"
			  })
			  return true;
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";
</style>
